package com.ztsoft.user.controller.project;

import com.ztsoft.user.pojo.dto.ModelFileFileDTO;
import com.ztsoft.user.pojo.entity.project.PModel;
import com.ztsoft.user.pojo.page.PageQuery;
import com.ztsoft.user.service.project.ModelService;
import com.ztsoft.user.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;

@Slf4j
@RestController
@CrossOrigin("*")
@RequestMapping("/model")
public class ModelController {
    @Resource
    private ModelService modelService;

    /**
     * 生成通知书
     */
    @GetMapping("/sendFile")
    public ResponseEntity<FileSystemResource> sendFile(ModelFileFileDTO modelFileFileDTO){
        File zipFile =   modelService.sendFile(modelFileFileDTO);
        if (zipFile == null || !zipFile.exists()) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        FileSystemResource resource = new FileSystemResource(zipFile);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + zipFile.getName() + "\"");
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");

        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(zipFile.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(resource);

    }
    /**
     * 查看
     */
    @GetMapping("/list")
    public Result<Object> list(PModel bo, PageQuery pageQuery) {
        return modelService.list(bo, pageQuery);
    }
    /**
     * 查看单个
     */
    @GetMapping("/{id}")
    public Result<Object> modelOne(@PathVariable String id) {
        return modelService.modelOne(id);
    }
    /**
     * 添加
     */
    @PostMapping()
    public Result<Object> add(@RequestBody PModel bo) {
        return modelService.add(bo);
    }
    /**
     * 添加
     */
    @PutMapping()
    public Result<Object> update(@RequestBody PModel bo) {
        return modelService.update(bo);
    }
    /**
     * 删除
     */
    @DeleteMapping ()
    public Result<Object> delete(String [] ids) {
        return modelService.delete(ids);
    }
}
